package cn.com.api.controller;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.com.api.entity.ApiReturnGoodsOrder;
import cn.com.api.requestpage.CancelReasonPage;
import cn.com.api.requestpage.OrderConfirmPage;
import cn.com.api.requestpage.SearchOrdersPage;
import cn.com.api.requestpage.SendOrderPage;
import cn.com.api.requestpage.ShopOrderPage;
import cn.com.api.service.ApiCarService;
import cn.com.api.service.ApiOrderService;
import cn.com.api.service.CheckService;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "order/")
public class ApiOrderController {
	@Autowired
	private ApiOrderService apiOrderService;
	@Autowired
	private CheckService checkService;
	@Autowired
	private ApiCarService apiCarService;

	private static Logger logger = Logger.getLogger(ApiOrderController.class);

	/**
	 * 订单--下订单confirm an order 结算
	 */
	@RequestMapping(value = "confirmOrder")
	public void confirmOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			/**
			 * 需要参数： u_id:用户名----1：用户真实姓名，手机号 2：默认收货地址 c_ids:购物车ids---商品放到购物车的记录
			 * -----店铺名+商品 -----计算总的价格出来
			 * 
			 * 
			 */
			String u_id = request.getParameter("u_id");
			String c_ids = request.getParameter("c_ids");// 分隔，以","分隔
			String token = request.getParameter("token");
			Map<String, String> params = new HashMap<String, String>();
			params.put("u_id", u_id);
			params.put("c_ids", c_ids);
			params.put("token", token);
			if (checkService.checkToken(u_id, token, map) && checkService.checkParams(params, map)) {
				apiOrderService.initOrder(u_id, c_ids, map);
			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/** 生产订单前校验收货地址 */
	@RequestMapping(value = "checkOrderAddress")
	public void checkOrderAddress(OrderConfirmPage orderConfirmPage, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			String u_id = orderConfirmPage.getU_id();
			String token = orderConfirmPage.getToken();

			if (checkService.checkToken(u_id, token, map)
					&& checkService.checkOrderAddressPage(orderConfirmPage, map)) {
				apiCarService.checkOrderAddress(orderConfirmPage, map);
			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 订单--确认订单confirm an order
	 */
	@RequestMapping(value = "confirmOrder1")
	public void confirmOrder1(OrderConfirmPage orderConfirmPage, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		/**
		 * u_id:用户名----1：用户真实姓名，手机号 2：默认收货地址 c_ids:购物车ids---商品放到购物车的记录
		 * -----店铺名+商品 -----计算总的价格出来--需要再比对一次 收获地址：address:id 备注：remark
		 * 付款方式：payType 优惠券：------暂时先不写 配送费：sendfee 在线支付立减优惠活动---不写先--- 订单总金额
		 * sumPrice
		 * 
		 * 需要校验：1-商品库存是否充足 2-后台计算金额与前台传过来的是否一致
		 * 
		 */
		String u_id = orderConfirmPage.getU_id();
		String token = orderConfirmPage.getToken();
		System.out.println(u_id + "-" + token);
		try {
			if (checkService.checkToken(u_id, token, map)
					&& checkService.checkRequestOrderConfirmPage(orderConfirmPage, map)) {// 校验入参
				// 校验c_id是否已生成订单---已生成则提示订单已生成---库存是否充足
				if (apiCarService.checkProducts(orderConfirmPage, map)) {
					apiOrderService.confirmOrder(orderConfirmPage, map);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 我的订单---用户端
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "myOrder")
	public void myOrders(SearchOrdersPage searchOrdersPage, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			Map<String, String> params = new HashMap<String, String>();
			String u_id = request.getParameter("u_id");
			String orderStatus = request.getParameter("orderStatus");
			String pageCount = request.getParameter("pageCount");
			String token = request.getParameter("token");
			params.put("u_id", u_id);
			params.put("orderStatus", orderStatus);
			params.put("pageCount", pageCount);
			params.put("token", token);
			if (checkService.checkToken(u_id, token, map) && checkService.checkMyOrder(searchOrdersPage, map)) {
				PageInfo page = new PageInfo();
				page.setCurrentPage(Integer.parseInt(pageCount));// 设置要读取的页
				apiOrderService.getMyOrders(params, map, page);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 订单详情----自营可退货，非自营不允许退货 -----取消订单（商家已接单不显示）----注意页面显示 -----申请退货 -----确认收货
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "orderInfo")
	public void getOrderInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			Map<String, String> params = new HashMap<String, String>();
			String u_id = request.getParameter("u_id");
			String titles = request.getParameter("titles");
			String token = request.getParameter("token");
			// 根据订单号返回订单详情
			params.put("u_id", u_id);
			params.put("titles", titles);
			params.put("token", token);
			if (checkService.checkToken(u_id, token, map) && checkService.checkParams(params, map)) {
				apiOrderService.getOrderInfo(titles, map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 申请取消订单
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "cancelOrder")
	public void cancelOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			Map<String, String> params = new HashMap<String, String>();
			String u_id = request.getParameter("u_id");
			String titles = request.getParameter("titles");
			String token = request.getParameter("token");
			params.put("u_id", u_id);
			params.put("titles", titles);
			params.put("token", token);
			/**
			 * 根据订单号 取消订单， 未付款，直接取消成功， 已付款，----自营（商家已接单---申请退货）
			 * ----非自营（商家已接单-----不允许退货）----这时候不显示 后台再判断确认下 商家为接单：取消原因，去取消
			 * 
			 */
			if (checkService.checkToken(u_id, token, map) && checkService.checkParams(params, map)) {
				apiOrderService.cancelOrder(titles, map);
			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 取消订单原因
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "cancelReason")
	public void cancelReason(CancelReasonPage cancelReasonPage, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String u_id = cancelReasonPage.getU_id();
		String token = cancelReasonPage.getToken();
		try {
			/**
			 * 根据订单号 取消订单， 未付款，直接取消成功， 已付款，----自营（商家已接单---申请退货）
			 * ----非自营（商家已接单-----不允许退货）----这时候不显示 后台再判断确认下
			 */
			if (checkService.checkToken(u_id, token, map)
					&& checkService.checkCancelReasonPage(cancelReasonPage, map)) {
				apiOrderService.cancelReason(cancelReasonPage, map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 确认收货
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "confirmReceive")
	public void confirmReceive(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			Map<String, String> params = new HashMap<String, String>();
			String u_id = request.getParameter("u_id");
			String titles = request.getParameter("titles");
			String token = request.getParameter("token");
			params.put("u_id", u_id);
			params.put("titles", titles);
			params.put("token", token);
			/**
			 * 确认收货：收货时间，订单完成(状态更新 enable=5 已完成) 物流状态：dd_status:6-用户确认收货
			 */
			if (checkService.checkToken(u_id, token, map) && checkService.checkParams(params, map)) {
				apiOrderService.confirmReceive(titles, map);
			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 订单状态---显示订单的物流状态
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "getUserOrderStatus")
	public void getUserOrderStatus(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			Map<String, String> params = new HashMap<String, String>();
			String u_id = request.getParameter("u_id");
			String titles = request.getParameter("titles");
			String token = request.getParameter("token");
			params.put("u_id", u_id);
			params.put("titles", titles);
			params.put("token", token);
			if (checkService.checkToken(u_id, token, map) && checkService.checkParams(params, map)) {
				apiOrderService.getOrderStatus(u_id, titles, map);
			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/** 充值有礼-到订单页面-init 未生产订单 */
	@RequestMapping(value = "chargeWithGiftInit")
	public void chargeWithGiftInit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		/**
		 * u_id: product_ids:充值有礼的货品 chargeMoney:300/500/1000----充值金额
		 */
		try {
			Map<String, String> params = new HashMap<String, String>();
			String u_id = request.getParameter("u_id");
			String token = request.getParameter("token");
			String product_ids = request.getParameter("product_ids");
			String chargeMoney = request.getParameter("chargeMoney");
			params.put("u_id", u_id);
			params.put("product_ids", product_ids);
			params.put("chargeMoney", chargeMoney);
			params.put("token", token);
			if (checkService.checkToken(u_id, token, map) && checkService.checkParams(params, map)) {
				apiOrderService.chargeWithGiftInit(params, map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/** 充值有礼-到订单页面-init 产生订单 */
	@RequestMapping(value = "chargeWithGiftConfirm")
	public void chargeWithGiftConfirm(OrderConfirmPage orderConfirmPage, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		/**
		 * u_id: product_ids:充值有礼的货品 chargeMoney:300/500/1000----充值金额
		 */
		try {
			Map<String, String> params = new HashMap<String, String>();
			String u_id = request.getParameter("u_id");
			String product_ids = request.getParameter("product_ids");
			String chargeMoney = request.getParameter("sumPrice");
			String token = request.getParameter("token");
			params.put("u_id", u_id);
			params.put("product_ids", product_ids);
			params.put("chargeMoney", chargeMoney);
			params.put("token", token);
			if (checkService.checkToken(u_id, token, map) && checkService.checkChargeGiftPage(orderConfirmPage, map)) {
				apiOrderService.chargeWithGiftConfirm(orderConfirmPage, map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 积分商城订单页面初始化
	 * 
	 * @param orderConfirmPage
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "initPointsOrder")
	public void initPointsOrder(OrderConfirmPage orderConfirmPage, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			Map<String, String> params = new HashMap<String, String>();
			String u_id = request.getParameter("u_id");
			String token = request.getParameter("token");
			String act_ids = request.getParameter("act_ids");
			params.put("u_id", u_id);
			params.put("token", token);
			params.put("act_ids", act_ids);
			if (checkService.checkToken(u_id, token, map)) {
				if (!Util.isNull(act_ids)) {
					if (apiCarService.checkOrderPoints(orderConfirmPage, map)) {
						apiOrderService.initPointsOrder(params, map);
					}
				} else {
					map.put(Utils.RESULT, "0002");
					map.put(Utils.MESSAGE, "act_ids不能为空!");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 积分商城--确认订单
	 * 
	 * @param orderConfirmPage
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "confirmPointsOrder")
	public void confirmPointsOrder(OrderConfirmPage orderConfirmPage, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			Map<String, String> params = new HashMap<String, String>();
			String u_id = request.getParameter("u_id");
			String token = request.getParameter("token");
			String act_ids = request.getParameter("act_ids");
			params.put("u_id", u_id);
			params.put("token", token);
			params.put("act_ids", act_ids);
			if (checkService.checkToken(u_id, token, map) && checkService.checkPointsOrderPage(orderConfirmPage, map)) {
				apiOrderService.confirmPointsOrder(orderConfirmPage, map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();

	}

	/****************** 商户端 *******************************************************************************/

	/**
	 * 我的订单---商户端----
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "shopOrder")
	public void shopOrders(SearchOrdersPage searchOrdersPage, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String u_id = searchOrdersPage.getShop_id();
		String token = searchOrdersPage.getToken();

		try {
			if (checkService.checkToken(u_id, token, map) && checkService.checkShopOrders(searchOrdersPage, map)) {

				apiOrderService.getShopOrders(searchOrdersPage, map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 添加退货订单
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/addReturnOrder")
	public void addReturnOrder(ApiReturnGoodsOrder ret, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String uid = request.getParameter("uid");
		String token = request.getParameter("token");
		try {
			if (checkService.checkToken(uid, token, map)) {
/*				if (apiReturnGoodsOrderService.isReturn(ret.getGid()) == 0) {
					Date date = new Date();
					SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					ret.setCreateDate(sFormat.format(date).toString());
					ret.setReturnStatus(0);
					// 判断是否超出规定的退货时间
					if (apiOrderService.checkReturnGoods(ret.getGid(), map)) {
						if (apiReturnGoodsOrderService.addReturnGoodsOrder(ret)) {
							map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
							map.put(Utils.MESSAGE, "success");
						}
					}
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
					map.put(Utils.MESSAGE, "不能重复申请退货");
				}*/
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 查询退货订单
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/returnOrder")
	public @ResponseBody Map<String, Object> returnOrder(HttpServletRequest request, HttpServletResponse response) {
		String uid = request.getParameter("uid");
		String token = request.getParameter("token");
		String status = request.getParameter("status");
		Map<String, Object> re = new HashMap<String, Object>();
		Map<String, List<Map<String, Object>>> map = new HashMap<String, List<Map<String, Object>>>();
		try {
			if (checkService.checkToken(uid, token, re)) {
				//map = apiReturnGoodsOrderService.searHistoryByStatus(uid, status.split(","));
				List<Map<String, Object>> res = new ArrayList<Map<String, Object>>();
				for (String str : map.keySet()) {
					Map<String, Object> data = new HashMap<String, Object>();
					System.out.println(str);
					String[] spl = str.split(",");
					data.put("shopName", spl[0]);
					data.put("orderNum", spl[1]);
					data.put("money", spl[2]);
					data.put("returnStatus", spl[3]);
					data.put("reason", spl[4]);
					data.put("orderTime", spl[5].split("\\.")[0]);
					data.put("return_order", spl.length == 7 ? spl[6] : "");
					data.put("goodsList", map.get(str));
					res.add(data);
				}
				re.put("datas", res);
				re.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				re.put(Utils.MESSAGE, "操作成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			re.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			re.put(Utils.MESSAGE, "系统异常！");
		}
		return re;
	}

	/**
	 * 商铺端的催单列表
	 * 
	 * @param searchOrdersPage
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "remindOrder")
	public void remindOrder(SearchOrdersPage searchOrdersPage, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String u_id = searchOrdersPage.getShop_id();
		String token = searchOrdersPage.getToken();
		try {
			if (checkService.checkToken(u_id, token, map) && checkService.checkShopOrders(searchOrdersPage, map)) {

				apiOrderService.getRemindOrder(searchOrdersPage, map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 商家确认接受订单
	 * 
	 * @param searchOrdersPage
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "receiveOrder")
	public void receiveOrder(ShopOrderPage shopOrderPage, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String u_id = shopOrderPage.getShop_id().toString();
		String token = shopOrderPage.getToken();

		try {
			if (checkService.checkToken(u_id, token, map) && checkService.checkShopOrderPage(shopOrderPage, map)) {

				apiOrderService.receiveOrder(shopOrderPage, map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 商家拒绝接受订单--相当于订单取消---推送消息给用户端
	 * 
	 * @param searchOrdersPage
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "refuseOrder")
	public void refuseOrder(ShopOrderPage shopOrderPage, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String u_id = shopOrderPage.getShop_id().toString();
		String token = shopOrderPage.getToken();

		try {
			if (checkService.checkToken(u_id, token, map) && checkService.checkShopOrderPage(shopOrderPage, map)) {
				if (Util.isNull(shopOrderPage.getCancelReason())) {
					map.put(Utils.RESULT, "0002");
					map.put(Utils.MESSAGE, "取消原因不能为空！");
				} else {
					apiOrderService.refuseOrder(shopOrderPage, map);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 商铺端搜索接口---订单号、收货地址、收货人电话
	 * 
	 * @param searchOrdersPage
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "shopSearchOrders")
	public void shopSearchOrders(SearchOrdersPage searchOrdersPage, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String u_id = searchOrdersPage.getShop_id();
		String token = searchOrdersPage.getToken();
		try {
			if (checkService.checkToken(u_id, token, map) && checkService.checkShopOrders(searchOrdersPage, map)) {

				apiOrderService.shopSearchOrders(searchOrdersPage, map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();

	}

	/****************** 配送端 *******************************************************************************/

	/**
	 * 配送端的订单
	 * 
	 * @param searchOrdersPage
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "sendOrders")
	public void sendOrders(SearchOrdersPage searchOrdersPage, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String u_id = searchOrdersPage.getU_id();
		String token = searchOrdersPage.getToken();
		try {
			if (checkService.checkToken(u_id, token, map) && checkService.checkMyOrder(searchOrdersPage, map)) {
				apiOrderService.getSendOrders(searchOrdersPage, map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 配送员接单
	 * 
	 * @param searchOrdersPage
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "sendReceiveOrder")
	public void sendReceiveOrder(SendOrderPage sendOrderPage, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String u_id = sendOrderPage.getU_id();
		String token = sendOrderPage.getToken();
		try {
			if (checkService.checkToken(u_id, token, map) && checkService.checkSendOrderPage(sendOrderPage, map)) {
				apiOrderService.sendReceiveOrders(sendOrderPage, map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 配送端查询订单接口：根据 订单号、地址(收货地址)、电话(收货人联系电话)
	 * 
	 * @param sendOrderPage
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "sendSearchOrders")
	public void sendSearchOrders(SearchOrdersPage searchOrdersPage, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String u_id = searchOrdersPage.getU_id();
		String token = searchOrdersPage.getToken();
		try {
			searchOrdersPage.setOrderStatus(9);
			if (checkService.checkToken(u_id, token, map) && checkService.checkMyOrder(searchOrdersPage, map)) {
				apiOrderService.sendSearchOrders(searchOrdersPage, map);
			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 配送员达到商家----已到商家---只修改配送任务表即可 status=3
	 * 
	 * @param sendOrderPage
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "senderArriveShop")
	public void senderArriveShop(SendOrderPage sendOrderPage, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String u_id = sendOrderPage.getU_id();
		String token = sendOrderPage.getToken();
		try {
			if (checkService.checkToken(u_id, token, map) && checkService.checkSendOrderPage(sendOrderPage, map)) {
				apiOrderService.senderArriveShop(sendOrderPage, map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 配送员取货---验证码
	 * 
	 * @param sendOrderPage
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "senderTakeGoods")
	public void senderTakeGoods(SendOrderPage sendOrderPage, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String u_id = sendOrderPage.getU_id();
		String token = sendOrderPage.getToken();
		try {
			if (checkService.checkToken(u_id, token, map)
					&& checkService.checkSendOrderPageIdentifyCode(sendOrderPage, map)) {
				// 取货成功 shipping：status=4(取货) dingdan dd_status=5
				apiOrderService.senderTakeGoods(sendOrderPage, map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 配送员 货已送达---货到付款，更新支付状态
	 * 
	 * @param sendOrderPage
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "sendGoodsOut")
	public void sendGoodsOut(SendOrderPage sendOrderPage, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String u_id = sendOrderPage.getU_id();
		String token = sendOrderPage.getToken();
		try {
			if (checkService.checkToken(u_id, token, map) && checkService.checkSendOrderPage(sendOrderPage, map)) {
				apiOrderService.sendGoodsOut(sendOrderPage, map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

}
